﻿using System;
using UnityEngine;
using UnityEngine.UI;
using BridgeShips.GUISystem;

namespace BridgeShips.Control {
    public class BuildingShipMap : GUIBehaviour {


        [SerializeField]
        private Window m_Window = null;

        [SerializeField]
        private Camera m_GUICamera;


        [Header( "Audio" )]

        [SerializeField]
        private SoundPlayer m_RefreshAudio;

        [SerializeField]
        private SoundPlayer m_SelectButtonAudio;

        private BuildingCategory[] m_Categories;

        private float m_CategoryScrollPos;

        private Button btn;

        private void Update() {
            if (!m_Window.IsOpen)
                return;
        }

        private void Start() {

            Player.Climate.AddStartTryer( TryStart_ClimateUI );
            Player.Climate.AddStopTryer( TryStop_ClimateUI );

            InventoryController.Instance.State.AddChangeListener( OnChanged_InventoryMapState );

            btn = transform.GetChild(3).GetComponent<Button>();
            btn.onClick.AddListener( BtnClick );
        }

        private void OnChanged_InventoryMapState() {
            var inventoryClosed = InventoryController.Instance.IsClosed;
            if (!inventoryClosed) {
                while (Player.Climate.Active)
                    Player.Climate.TryStop();
            }
        }

        //Climate UI

        private bool TryStart_ClimateUI() {
            if (!InventoryController.Instance.IsClosed)
                return false;
            m_Window.Open();
            return true;
        }

        private bool TryStop_ClimateUI() {
            m_Window.Close();
            return true;
        }

        void BtnClick() {
            GameController.LocalPlayer.Climate.TryStop();
        }
    }
}
